package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.dto.DishDto;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.DishFlavor;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.DishService;
import com.example.reggie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);

        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        IPage<DishDto> dishDtoPage = dishService.toDtoPage(page,pageSize,name);
        return R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable long id) {
        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);
        return R.success(byIdWithFlavor);
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        ids.forEach(item -> {
            System.out.println(item.toString());
        });

        dishService.removeByIds(ids);
        return R.success("删除成功");

    }

    //    @GetMapping("/list")
//    public R list(Dish dish){
//        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper();
//        lqw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
//        //只查询状态为1的菜品 即起售的菜品
//        lqw.eq(Dish::getStatus,1);
//        List<Dish> list = dishService.list(lqw);
//        return R.success(list);
//    }
    @GetMapping("/list")
    public R list(Dish dish) {
        List<DishDto> dishDtoList = dishService.toDtoList(dish);
        return R.success(dishDtoList);
    }


    @PostMapping("status/{code}")
    public R status(@RequestParam List<Long> ids, @PathVariable int code) {
        dishService.updateStatus(ids, code);
        return R.success(code == 1 ? "启售成功！" : "停售成功！");
    }
}
